 
<?php

/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright 2011
 * @license    All rights reserved
 **/

class listeImportSrv {    
    /**
     * Fonction permettant de verifeir si le mail du vendeur existe
     * @return boolean true if exists false if not
     *
     */
    public static function liste () {
        self::upd();

        $sql = " SELECT IdOccasion, Occas_IDorigine,";
        $sql.= " CONCAT(Occas_Marque, Occas_Modele, Occas_MiseCirculation, Occas_NumImmatricul, Occas_Kilometrage, Occas_Vendeur,Occas_Prix) AS critere"; 
        $sql.= " FROM `ao_voiture_occasion` WHERE  1";
        $sql.= " AND (Occas_valide = '1' OR Occas_valide = '2') AND Occas_Estsaisie = '1'";
        $sql.= " ORDER BY critere";
       
        $jdb        = jDb::getDbWidget();
        $result     = $jdb->fetchAll($sql);

        return $result;

    }

    public static function upd() {
        $sql = " UPDATE  ao_voiture_occasion SET Occas_IDorigine = '' WHERE Occas_IDorigine IS NULL";
        $cnx = jDb::getConnection();
        $cnx->exec($sql);
    }

    /**
     * Fonction permettant de verifeir si le mail du vendeur existe
     * @return boolean true if exists false if not
     *
     */
    public static function AnnonceJauneToDelete ($tOccasId) {
        $cnx    = jDb::getConnection();

        $sql = " SELECT IdOccasion FROM ao_voiture_occasion WHERE Occas_Idorigine = 'AJA' AND IdOccasion IN (".implode(',', $tOccasId).")";
        $rs   = $cnx->query($sql);

        $result = array();
        while($row = $rs->fetch()){
			$result[] = $row->IdOccasion;
        }

		return $result;

    }


    /**
     * Fonction permettant de supprimer les doublons
     *
     */
    public static function deleteDoublon ($toDelete) {

         $sql = "DELETE FROM  ao_voiture_occasion WHERE IdOccasion IN (".implode(',', $toDelete).")";
         $cnx = jDb::getConnection();
         $cnx->exec($sql);

    }

    /**
     * Vérifie si code partenaire existe dans un tableau
     * @return mixed clé du tableau si oui false si non
     *
     */
    public static function checkPartenaire ($tab, $idOccas, $partenaire = '') {

         if (is_array($tab) && sizeof($tab)) {
             if (in_array($partenaire, $tab)) {
                 foreach($tab as $k=>$item) {
                    if ($partenaire == $item) {
                        unset($idOccas[$k]);
                        if ($partenaire) return $idOccas;
                    }
                 }
             }
             return $idOccas;
         }
         return false;
    }

    /**
     * Trouve le code partenaire à prioriser
     * @return string : code partenaire
     *
     */
    public static function findPartToCheck ($tab) {
        $part = "";
         if (is_array($tab) && sizeof($tab)) {
             if (in_array('AJA', $tab)) $part = 'AJA';
                
             else if  (in_array('WCP', $tab))       $part = 'WCP';
             else if  (in_array('ADP', $tab))       $part = 'ADP';
             else if  (in_array('AUS', $tab))       $part = 'AUS';
             else if  (in_array('ANA', $tab))       $part = 'ANA';
             else if  (in_array('ANA_PART', $tab))  $part = 'ANA_PART';
             else if  (in_array('CAD_PRO', $tab))   $part = 'CAD_PRO';
             else if  (in_array('HFCA', $tab))      $part = 'HFCA';
             else if  (in_array('AAC', $tab))       $part = 'AAC';
             else if  (in_array('CAR', $tab))       $part = 'CAR';
         }

         return $part;
    }





}
